package sit.engreal.leetcode.problems300;

import java.util.ArrayList;

/**
 * <a href="https://leetcode.cn/problems/first-bad-version/">278. 第一个错误的版本</a>
 */
public class LeetCode278 {
    public static void main(String[] args) {
        class VersionControl {
            public int minBadVersion;
            public final ArrayList<Integer> list = new ArrayList<>();

            boolean isBadVersion(int version) {
                list.add(version);
                return version >= minBadVersion;
            }

        }

        class Solution extends VersionControl {
            public int firstBadVersion(int n) {
                int left = 1;
                int right = n;
                while (left < right) {
                    int i = left + (right - left) / 2;
                    if (isBadVersion(i)) {
                        right = i;
                    } else {
                        left = i + 1;
                    }
                }
                return right;
            }
        }

        final Solution solution = new Solution();
        solution.minBadVersion = 4;
        System.out.println(solution.firstBadVersion(5));
        System.out.println(solution.list);
    }
}

